home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / OldSrc / CH3 / SRC / PALWATCH.FRM < prev    next >
Text File  |  1997-01-02  |  8KB  |  299 lines

  1. VERSION 4.00
  2. Begin VB.Form PalWatchForm 
  3.    Caption         =   "PalWatch"
  4.    ClientHeight    =   2460
  5.    ClientLeft      =   6810
  6.    ClientTop       =   975
  7.    ClientWidth     =   2460
  8.    Height          =   3150
  9.    Left            =   6750
  10.    LinkTopic       =   "Form1"
  11.    ScaleHeight     =   164
  12.    ScaleMode       =   3  'Pixel
  13.    ScaleWidth      =   164
  14.    Top             =   345
  15.    Width           =   2580
  16.    Begin VB.Timer ColorTimer 
  17.       Interval        =   1000
  18.       Left            =   120
  19.       Top             =   120
  20.    End
  21.    Begin VB.PictureBox Pict 
  22.       AutoRedraw      =   -1  'True
  23.       AutoSize        =   -1  'True
  24.       Height          =   300
  25.       Left            =   0
  26.       Picture         =   "PALWATCH.frx":0000
  27.       ScaleHeight     =   16
  28.       ScaleMode       =   3  'Pixel
  29.       ScaleWidth      =   16
  30.       TabIndex        =   0
  31.       Top             =   0
  32.       Width           =   300
  33.    End
  34.    Begin VB.Menu mnuFile 
  35.       Caption         =   "&File"
  36.       Begin VB.Menu mnuFileExit 
  37.          Caption         =   "E&xit"
  38.       End
  39.    End
  40.    Begin VB.Menu mnuColor 
  41.       Caption         =   "(0, 0, 0)"
  42.       NegotiatePosition=   3  'Right
  43.    End
  44. End
  45. Attribute VB_Name = "PalWatchForm"
  46. Attribute VB_Creatable = False
  47. Attribute VB_Exposed = False
  48. Option Explicit
  49.  
  50. Const NO_COLOR = -1
  51.  
  52. Dim LogicalPalette As Integer
  53.  
  54. Dim SysPalSize As Integer
  55. Dim NumStaticColors As Integer
  56.  
  57. Dim SelectedI As Integer
  58. Dim SelectedJ As Integer
  59. Dim SelectedColor As Integer
  60. Dim SelectedR As Integer
  61. Dim SelectedG As Integer
  62. Dim SelectedB As Integer
  63.  
  64. Dim dx As Integer
  65. Dim dy As Integer
  66.  
  67. ' ***********************************************
  68. ' Load the Pict palette with PC_EXPLICIT entries
  69. ' so they match the system palette.
  70. ' ***********************************************
  71. Sub LoadSystemPalette()
  72. Dim palentry(0 To 255) As PALETTEENTRY
  73. Dim i As Integer
  74.  
  75.     ' Make the logical palette as big as possible.
  76.     LogicalPalette = Pict.Picture.hPal
  77.     If ResizePalette(LogicalPalette, SysPalSize) = 0 Then
  78.         Beep
  79.         MsgBox "Error resizing the palette.", _
  80.             vbCritical
  81.         End
  82.     End If
  83.     
  84.     ' Flag all palette entries as PC_EXPLICIT.
  85.     ' Set peRed to the system palette indexes.
  86.     For i = 0 To SysPalSize - 1
  87.         palentry(i).peRed = i
  88.         palentry(i).peFlags = PC_EXPLICIT
  89.     Next i
  90.     
  91.     ' Update the palette (ignore return value).
  92.     i = SetPaletteEntries(LogicalPalette, 0, SysPalSize, palentry(0))
  93. End Sub
  94.  
  95. ' ***********************************************
  96. ' Fill the system picture with all the palette
  97. ' colors, hatching the static colors.
  98. ' ***********************************************
  99. Sub FillPict()
  100. Dim i As Integer
  101. Dim j As Integer
  102. Dim clr As Integer
  103. Dim oldfill As Integer
  104. Dim olddraw As Integer
  105.  
  106.     Pict.Cls
  107.     
  108.     ' Display the colors using palette indexing.
  109.     dx = Pict.ScaleWidth / 16
  110.     dy = Pict.ScaleHeight / 16
  111.     clr = 0
  112.     For i = 0 To 15
  113.         For j = 0 To 15
  114.             Pict.Line (j * dx, i * dy)-Step(dx, dy), _
  115.                 clr + &H1000000, BF
  116.             clr = clr + 1
  117.         Next j
  118.     Next i
  119.     
  120.     ' Hatch the static colors.
  121.     oldfill = Pict.FillStyle
  122.     olddraw = Pict.DrawMode
  123.     Pict.FillStyle = vbDownwardDiagonal
  124.     Pict.DrawMode = vbInvisible
  125.     
  126.     Pict.Line (0, 0)-Step((NumStaticColors \ 2) * dx - 1, dy - 1), , B
  127.     Pict.Line (j * dx, i * dy)-Step(-(NumStaticColors \ 2) * dx, -dy), , B
  128.     
  129.     Pict.FillStyle = oldfill
  130.     Pict.DrawMode = olddraw
  131.  
  132.     ' Highlight the previously selected color.
  133.     SelectedColor = NO_COLOR
  134.     SelectColor SelectedI, SelectedJ
  135. End Sub
  136.  
  137. ' ***********************************************
  138. ' Select the color at the indicated position.
  139. ' ***********************************************
  140. Sub SelectColor(ByVal i As Integer, ByVal j As Integer)
  141. Const GAP1 = 1
  142. Const GAP2 = 2
  143. Const DRAW_WID = 2
  144.  
  145. Dim oldmode As Integer
  146. Dim oldwid As Integer
  147.  
  148.     oldmode = Pict.DrawMode
  149.     oldwid = Pict.DrawWidth
  150.     Pict.DrawMode = vbInvert
  151.     Pict.DrawWidth = DRAW_WID
  152.     
  153.     ' Unhighlight the previously selected color.
  154.     If SelectedColor <> NO_COLOR Then _
  155.         Pict.Line (SelectedJ * dx + GAP1, SelectedI * dx + GAP1)-Step(dx - GAP2, dx - GAP2), , B
  156.     
  157.     ' Record the new color.
  158.     SelectedI = i
  159.     SelectedJ = j
  160.     SelectedColor = i * 16 + j
  161.  
  162.     ' Highlight the new color.
  163.     Pict.Line (SelectedJ * dx + GAP1, SelectedI * dx + GAP1)-Step(dx - GAP2, dx - GAP2), , B
  164.     Pict.DrawMode = oldmode
  165.     Pict.DrawWidth = oldwid
  166.  
  167.     ' Display the color's components in mnuColor.
  168.     ShowColorValue
  169. End Sub
  170.  
  171.  
  172. ' ***********************************************
  173. ' If the selected color's components have
  174. ' changed, display the new values in mnuColor.
  175. ' ***********************************************
  176. Sub ShowColorValue()
  177. Dim palentry As PALETTEENTRY
  178. Dim status As Integer
  179.  
  180.     status = GetSystemPaletteEntries(Pict.hdc, SelectedColor, 1, palentry)
  181.     If palentry.peRed <> SelectedR Or _
  182.        palentry.peGreen <> SelectedG Or _
  183.        palentry.peBlue <> SelectedB Then
  184.             mnucolor.Caption = "(" & _
  185.                 Format$(palentry.peRed) & "," & _
  186.                 Str$(palentry.peGreen) & "," & _
  187.                 Str$(palentry.peBlue) & ")"
  188.     End If
  189. End Sub
  190.  
  191. ' ***********************************************
  192. ' Make sure the selected color's components are
  193. ' up to date.
  194. ' ***********************************************
  195. Private Sub ColorTimer_Timer()
  196.     ShowColorValue
  197. End Sub
  198. Private Sub Form_Load()
  199.     ' Make sure the screen supports palettes.
  200.     If Not GetDeviceCaps(hdc, RASTERCAPS) And RC_PALETTE Then
  201.         Beep
  202.         MsgBox "This monitor does not support palettes.", _
  203.             vbCritical
  204.         End
  205.     End If
  206.  
  207.     ' See how big the system palette is.
  208.     SysPalSize = GetDeviceCaps(hdc, SIZEPALETTE)
  209.  
  210.     NumStaticColors = GetDeviceCaps(hdc, NUMRESERVED)
  211.         
  212.     ' Load the system palette.
  213.     LoadSystemPalette
  214. End Sub
  215.  
  216. ' ***********************************************
  217. ' Make the picture as large as possible.
  218. ' ***********************************************
  219. Private Sub Form_Resize()
  220. Dim wid As Single
  221. Dim hgt As Single
  222.  
  223.     If WindowState = vbMinimized Then Exit Sub
  224.     
  225.     wid = ScaleWidth - 2 * Pict.Left
  226.     If wid < 10 Then wid = 10
  227.     hgt = ScaleHeight - 2 * Pict.Top
  228.     If hgt < 10 Then hgt = 10
  229.     Pict.Move Pict.Left, Pict.Top, wid, hgt
  230.     
  231.     ' Display the colors.
  232.     FillPict
  233. End Sub
  234.  
  235.  
  236.  
  237. ' ***********************************************
  238. ' Select the color the user clicked on.
  239. ' ***********************************************
  240. Private Sub Pict_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
  241. Dim i As Integer
  242. Dim j As Integer
  243.  
  244.     i = Y \ dx
  245.     j = X \ dy
  246.     SelectColor i, j
  247. End Sub
  248.  
  249. Private Sub mnuFileExit_Click()
  250.     Unload Me
  251. End Sub
  252.  
  253.  
  254. ' ***********************************************
  255. ' Allow the user to select a new color with the
  256. ' arrow keys.
  257. ' ***********************************************
  258. Private Sub Pict_KeyDown(KeyCode As Integer, Shift As Integer)
  259. Dim i As Integer
  260. Dim j As Integer
  261.  
  262.     i = SelectedI
  263.     j = SelectedJ
  264.  
  265.     Select Case KeyCode
  266.         Case vbKeyDown
  267.             i = i + 1
  268.             If i * 16 + j >= SysPalSize Then i = 0
  269.         
  270.         Case vbKeyUp
  271.             i = i - 1
  272.             If i < 0 Then
  273.                 i = (SysPalSize - 1) \ 16
  274.                 If i * 16 + j >= SysPalSize Then _
  275.                     i = i - 1
  276.             End If
  277.         
  278.         Case vbKeyLeft
  279.             j = j - 1
  280.             If j < 0 Then
  281.                 j = 15
  282.                 If i * 16 + j >= SysPalSize Then _
  283.                     j = SysPalSize - 1 - i * 16
  284.             End If
  285.         
  286.         Case vbKeyRight
  287.             j = j + 1
  288.             If j > 15 Or _
  289.                 i * 16 + j >= SysPalSize Then _
  290.                     j = 0
  291.         
  292.     End Select
  293.     
  294.     SelectColor i, j
  295. End Sub
  296.  
  297.  
  298.  
  299.